package com.panels;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import com.dataModel.Quran;
import com.gui.components.indexTabel.IndexTable;
import com.gui.components.search.SearchPanel;

public class NorthPanel extends JPanel implements  ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 2480465039870624409L;
	private JTextField pageNumberTextField;
	private JButton searchButton;
	private JButton statisticsButton;
	private JButton mySummarized;
	private JButton myFavorits;
	private JButton index;

	
	public NorthPanel() {
		statisticsButton = new JButton("Reports");
		searchButton = new JButton("Search");
		mySummarized = new JButton("My Summarized");
		myFavorits = new JButton("My Favorites");
		index = new JButton("Index");
		pageNumberTextField=new JTextField(6);
		pageNumberTextField.setHorizontalAlignment(JTextField.HORIZONTAL);
		
		index.addActionListener(this);
		searchButton.addActionListener(this);
		myFavorits.addActionListener(this);
		
		pageNumberTextField.addKeyListener(new KeyListener(){

			public void keyPressed(KeyEvent keyEvent) {
				if(keyEvent.getKeyCode()==KeyEvent.VK_ENTER)
				{
					int pageNumber=0;
					try
					{
					 pageNumber=Integer.parseInt(pageNumberTextField.getText());
					}catch(NumberFormatException numberFormatException)
					{
						pageNumberTextField.setText("");
					}
					if (pageNumber < 1 || pageNumber > Quran.TAHA_OSMAN_PAGE_NUMBERS) {
						return;
					}
					MainPanel.getMainPanel().getPagesPanel().displayPage(pageNumber);
				}
				
			}

			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}

			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}});

		initGui();
	}

	void initGui() {
		this.setLayout(new BorderLayout());
		JPanel centerPanel = new JPanel();
		JPanel leftPanel = new JPanel();
		JPanel rightPanel = new JPanel();
		
		GridBagConstraints textFiledConstraints=new GridBagConstraints();
		//textFiledConstraints.
		centerPanel.add(pageNumberTextField,textFiledConstraints);
		
		
		leftPanel.add(mySummarized);
		leftPanel.add(myFavorits);
		
		rightPanel.add(index);
		rightPanel.add(searchButton);
		rightPanel.add(statisticsButton);
		
		this.add(centerPanel,BorderLayout.CENTER);
		this.add(rightPanel,BorderLayout.EAST);
		this.add(leftPanel,BorderLayout.WEST);
		
		
	}

	public void actionPerformed(ActionEvent e) {
	
		if(e.getSource()==index)
		{
			JDialog indexDialog=new JDialog();
			indexDialog.setSize(420, 650);
			String columnName[]={"Number","Sourah","Place Of revliance","Page Number"};
			
			String [][]rows={{"1","2","3","4"},{"5","6","7","8"}};
			//indexDialog.add(new JTable(rows,columnName));
			indexDialog.add(new JScrollPane(new IndexTable(indexDialog)));
			indexDialog.setVisible(true);
		//	JOptionPane.showMessageDialog(this, new IndexTable());
		}
		else
			if(e.getSource()==searchButton)
			{
				JDialog searchDialog=new JDialog();
				searchDialog.setSize(800, 650);
				searchDialog.add(new SearchPanel());
				searchDialog.setVisible(true);
			}
			else
				if(e.getSource()==myFavorits)
				{
					JDialog myFavoritsDialog=new JDialog();
					myFavoritsDialog.setSize(800, 650);
					myFavoritsDialog.add(new MyList());
					myFavoritsDialog.setVisible(true);
				}
		
	}

	public JTextField getPageNumberTextField() {
		return pageNumberTextField;
	}

}
